package com.mmlc.meimei.lichengkongjian.Adapter;

import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;

import com.mmlc.inter.ViewPagerAdapter;

import java.util.List;

public class CirculationAdapter<T extends View> extends ViewPagerAdapter<T> {
    private final List<T> views;

    public CirculationAdapter(List<T> views) {
        super(views);
        this.views = views;
    }

    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        if (views.get(position % views.size()).getParent() != null) {
            ((ViewGroup) views.get(position % views.size())
                    .getParent()).removeView(views.get(position
                    % views.size()));
        }
        ((ViewPager) container).addView(
                views.get(position % views.size()), 0);
        return views.get(position % views.size());
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        ((ViewPager) container).removeView(views.get(position % views.size()));
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1;
    }

}
